﻿using IShoppingBll;
using IShoppingDal;
using ShoppingEntity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ShoppingBll
{
    public class SalesrRecordBll : BaseBll<SalesrRecord>, ISalesrRecordBll
    {
        private IOrderInfoDal _orderInfoDal;
        private ICommodityInfoDal _commodityInfoDal;
        private IUserInfoDal _userInfoDal;
        private IDistributionInfoDal _distributionInfoDal;
        private ISalesrRecordDal _salesrRecordDal;
        private ICommodityTypeDal _commodityTypeDal;
        private IReturnsDal _returns;

        public SalesrRecordBll(IOrderInfoDal orderInfoDal, ICommodityInfoDal commodityInfoDal, IUserInfoDal userInfoDal, IDistributionInfoDal distributionInfoDal, ISalesrRecordDal salesrRecordDal, ICommodityTypeDal commodityTypeDal , IReturnsDal returns) : base(salesrRecordDal)
        {
            _orderInfoDal = orderInfoDal;
            _commodityInfoDal = commodityInfoDal;
            _userInfoDal = userInfoDal;
            _distributionInfoDal = distributionInfoDal;
            _salesrRecordDal = salesrRecordDal;
            _commodityTypeDal = commodityTypeDal;
            _returns = returns;
        }

        /// <summary>
        /// 获取退货商品的名字、数量
        /// </summary>
        /// <returns></returns>
        public object GetReturnRecord()
        {
            ////退货商品记录查询
            var returns = _returns.GetEntitiesDb();

            ////商品表数据查询
            var commodityInfo = _commodityInfoDal.GetEntitiesDb().AsEnumerable();
            //查询订单表数据信息
            var orderInfo = _orderInfoDal.GetEntitiesDb().AsEnumerable();

            //返回商品名字及对应的统计数量
            var Temp = (from o in returns
                        join cd in commodityInfo
                        on o.GoodsId equals cd.ID into ocTemp
                        from oc in ocTemp.DefaultIfEmpty()

                        join cr in orderInfo
                        on o.GoodsId equals cr.ID into crTemp
                        from cor in crTemp.DefaultIfEmpty()

                        select new
                        {
                            oc.ID,
                            oc.CommodityName,
                            o.GoodsId,
                            cor.Num

                        }).ToList().OrderBy(m => m.Num).Take(10);

            //按照商品名字分组统计退货商品数量
            var res = Temp.ToList().GroupBy(t => t.GoodsId).Select(t =>
            {

                return new
                {
                    GoodsId = t.Key,
                    Sum = t.Sum(c => c.Num),
                    CommodityName = t

                };
            }).ToList();

            


            return res;
        }


        /// <summary>
        /// 获取商品名字、数量
        /// </summary>
        /// <param name="commodityName"></param>
        /// <param name="num"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        public object GetSalesrRecord()
        {
            ////商品记录查询
            var salesrRecord = _salesrRecordDal.GetEntitiesDb();

            ////商品表数据查询
            var commodityInfo = _commodityInfoDal.GetEntitiesDb().AsEnumerable();



            //按照商品名字分组统计商品数量
            var res = salesrRecord.ToList().GroupBy(t => t.GoodsId).Select(t =>
             {

                 return new
                 {
                     GoodsId = t.Key,
                     Sum = t.Sum(c => c.Num)
                     
                     
                 };
             }).ToList();

            //返回商品名字及对应的统计数量
            var Temp = (from o in res
                       join cd in commodityInfo
                       on o.GoodsId equals cd.ID into ocTemp
                       from oc in ocTemp.DefaultIfEmpty()

                       select new
                       {
                           oc.ID,
                           oc.CommodityName,
                           o.Sum
                           
                       }).ToList().OrderBy(m=>m.Sum).Take(10);


            return Temp;

        }


        /// <summary>
        /// 获取商品统计展示分页
        /// </summary>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <param name="count"></param>
        /// <param name="commodityName"></param>
        /// <returns></returns>
        public object GetSalesrRecordPageList(int page, int limit, out int count, string commodityName)
        {
            //商品记录查询
            var salesrRecord = _salesrRecordDal.GetEntitiesDb();

            //商品表数据查询
            var commodityInfo = _commodityInfoDal.GetEntitiesDb().AsEnumerable();

            if (!string.IsNullOrEmpty(commodityName))
            {
                commodityInfo = _commodityInfoDal.GetEntitiesDb().Where(c => c.CommodityName.Contains(commodityName));
            }


            var Temp = from o in salesrRecord
                       join cd in _commodityInfoDal.GetEntitiesDb()
                       on o.GoodsId equals cd.ID into ocTemp
                       from oc in ocTemp.DefaultIfEmpty()

                       join ct in _commodityTypeDal.GetEntitiesDb()
                       on oc.CategoryId equals ct.ID into ctTemp
                       from oct in ctTemp.DefaultIfEmpty()

                       join or in _orderInfoDal.GetEntitiesDb().Where(u => !u.IsDelete)
                       on o.OrderID equals or.ID into orTemp
                       from oor in orTemp.DefaultIfEmpty()

                       select new
                       {
                           o.ID,
                           oc.CommodityName,
                           oct.CategoryName,
                           o.Num,
                           oor.Amount,
                           o.OrderTime
                       };

            //查询出来数据的数量
            count = Temp.Count();
            //分页
            Temp = Temp.OrderBy(c => c.OrderTime).Skip((page - 1) * limit).Take(limit);

            var list = Temp.ToList().Select(c => new
            {
                c.ID,
                c.CommodityName,
                c.Num,
                c.Amount,
                c.CategoryName,
                OrderTime = c.OrderTime.ToString("yyyy-MM-dd HH:mm:ss"),

            });

            return list;
        }
    }
}
